﻿using GN.Pay.TradeChannel.DefaultImpl;
using System;

namespace GN.Pay.TradeChannel.WeiXin.Response
{
    /// <summary>
    /// 退款响应
    /// </summary>
    [Serializable]
    class RefundResponse : AResponse
    {
        public RefundResponse(string xml)
            : base(xml)
        {
        }

        /// <summary>
        /// 应用Id
        /// </summary>
        public string AppId { get; set; }

        /// <summary>
        /// 商户号
        /// </summary>
        public string Mch_Id { get; set; }

        /// <summary>
        /// 设备信息
        /// </summary>
        public string Device_Info { get; set; }

        /// <summary>
        /// 交易类型
        /// </summary>
        public string Trade_Type { get; set; }

        /// <summary>
        /// 支付id
        /// </summary>
        public string Prepay_Id { get; set; }

        /// <summary>
        /// 商户退款单号
        /// </summary>
        public string Out_Refund_No { get; set; }

        /// <summary>
        /// 商户传给微信的订单号(与 Transaction_Id 至少一个不能为空)
        /// </summary>
        public string Out_Trade_No { get; set; }

        /// <summary>
        /// 退款总金额，单位为分，只能为整数
        /// </summary>
        public long Refund_Fee { get; set; }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public ITradeRefundApplyResult ToResult(ITradeChannel channel)
        {
            if (IsFullSuccess())
            {
                return ToResult(channel, TradeApplyResultState.Complete_Success, "退款成功");
            }
            if ("NOTENOUGH".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, TradeApplyResultState.Balance_Insufficient_Error, "账户余额不足");
            }
            if ("TRADE_OVERDUE".Equals(this.Err_Code, StringComparison.InvariantCultureIgnoreCase))
            {
                return ToResult(channel, TradeApplyResultState.Info_Error, "订单已经超过退款期限");
            }
            if (!IsReturnSuccess())
            {
                return ToResult(channel, TradeApplyResultState.Apply_Error, Return_Msg);
            }
            if (!IsResultSuccess())
            {
                if (string.IsNullOrWhiteSpace(Err_Code_Des))
                {
                    return ToResult(channel, TradeApplyResultState.Apply_Error, Err_Code);
                }
                else
                {
                    return ToResult(channel, TradeApplyResultState.Apply_Error, Err_Code_Des);
                }
            }
            throw channel.ThrowChannelValidationException("响应发生未知异常，退款申请结果不是已知的状态");
        }

        private ITradeRefundApplyResult ToResult(ITradeChannel channel, TradeApplyResultState state, string stateMsg)
        {
            return new TradeRefundApplyResult(channel,
                  this.Out_Refund_No,
                  this.Out_Trade_No,
                 GetMoneyForGeneral(this.Refund_Fee),
                 0m, null,
                 state,
                 stateMsg);
        }
    }
}
